// pages/recharge.js
var app = getApp()
import ajaxhelper from '../../utils/ajaxhelper.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    rechargeList:[],
    rechargeId:'',
    rechargeName:'',
    rechargeAmount:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getListData();//获取充值列表
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var rechargeAmount = e.currentTarget.dataset.amount;
    var cur = e.currentTarget.dataset.current;
    if (this.data.currentTab == cur) { return false; }
    else {
      this.setData({
        currentTab: cur,
        rechargeId: id,
        rechargeName:name,
        rechargeAmount,
      })
    }
    console.log(that.data.rechargeId)
  },
  // 列表数据渲染
  getListData: function () {
    let param = {};
    var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    ajaxhelper.get(app.globalData.frontJSHost + 'rechargeActivity/selectRechargeActivityList?token=' + encodeURIComponent(token), param, this, this.setListData)
  },
  setListData(res) {
    console.log(res);
    var that=this;
    if (res.result && res.resultStatus == 200) {
      that.setData({
        rechargeList:res.data,
        rechargeId: res.data[0].id,//默认第一项id
        rechargeName: res.data[0].name,
        rechargeAmount: res.data[0].amountRecharge
      })
    }else{
      wx.showToast({
        title: !!res.message.global ? res.message.global : "请求出错",
        icon: "loading"
      })
    }
  },
  // 去充值
  goPay(){
    var that = this;
    var payChannel = 'WXPAY_MINI';//
    var sourceId = that.data.rechargeId;//
    var sourceName = that.data.rechargeName;//
    var priceFen = that.data.rechargeAmount;
    var params = {
      sourceId: sourceId,
      sourceName: sourceName,
      sourceType: 'recharge',
      priceFen: priceFen,
      buyCount:1,
      payChannel,
    }
   var token = (wx.getStorageSync('Token_date') && wx.getStorageSync('Token_date').Token) || '';
    console.log(params)
    console.log(token)
    ajaxhelper.post(app.globalData.frontJSHost + 'rechargeActivity/recharge?token=' + encodeURIComponent(token), params, this, function (res) {
      console.log(res)
      if (res.result && res.resultStatus == 200) {
        //0元不用掉支付
        if ((res.data.data && !res.data.data.params.hasOwnProperty('errorCode')) || res.data.paySuccess) {
          //如果没有绑定过 下订单之后就要存储新的绑定值
          var orderId = res.data.order && res.data.order.orderId;//订单id
          if (res.data.paySuccess) {
            that.paySuccessFun(sourceId, name, totalPrice, orderId, mediaType);
            return false
          }
          var payData = res.data.data.params;
          let nonceStr = payData.nonceStr;
          let timeStamp = payData.timeStamp;
          let packageValue = payData.package;
          let signPay = payData.signType;
          let paySign = payData.paySign;
          wx.requestPayment({
            timeStamp: timeStamp,
            nonceStr: nonceStr,
            package: packageValue,
            signType: signPay,
            paySign: paySign,
            success: function (res) {
              console.log(res)
              // /////
              // if (that.data.buyType=='BUY'){
              //   wx.redirectTo({
              //     url: '../successPurchase/successPurchase?name=' + name + '&price=' + totalPrice + '&id=' + orderId + '&mediaType=' + mediaType + '&sourceId=' + sourceId,
              //   })
              // } else if (that.data.buyType == 'GIFT'){
              //   // 如果是赠送进入的，支付成功跳转到赠送页面
              //   var unitPrice = that.data.courseItem.currentPrice;//单价
              //   var count = this.data.count;//购买数量
              //   var chapterCount = this.data.chapterCount;//集数
              //   wx.redirectTo({
              //     url: '../paySuccess/paySuccess?name=' + name + '&price=' + totalPrice + '&mediaType=' + mediaType + '&sourceId=' + sourceId + '&unitPrice=' + unitPrice + '&count=' + count + '&chapterCount=' + chapterCount,
              //   })
              // }
            },
            fail: function (res) {
              console.log(res)
              wx.redirectTo({
                url: '../rechargeStatus/rechargeStatus',
              })

            }
          })
        } else {
          wx.showToast({
            title: !!res.data.data.params.errorCodeDesc ? res.data.data.params.errorCodeDesc : "订单出错",
            icon: "loading"
          })
        }
      } else {
        wx.showToast({
          title: !!res.message.global ? res.message.global : "请求出错",
          icon: "loading"
        })
      }

    })
  }
  
})